package com.dahua.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.dahua.ui.user.RegisterAct;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;


public class AppApplication extends Application {
    private static AppApplication _context;
    public static RegisterAct registerAct;


    /**
     * 当前Acitity个数
     */
    private int activityAount = 0;
    boolean isForeground = true;


    @Override
    public void onCreate() {
        super.onCreate();
        _context = this;
//        腾讯Bugly  第三个参数dubug模式建议为true,发布改成false
        CrashReport.initCrashReport(getApplicationContext(), "c7e340ac5d", false);
        JPushInterface.setDebugMode(false);//极光推送设置开 启日志,发布时请关闭日志
        JPushInterface.init(this);//初始化推送
        registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    public static List<Activity> aliveActivitys = new ArrayList<Activity>();

    public static void finishAllActivity() {
        for (int i = 0; i < aliveActivitys.size(); i++) {
            if (aliveActivitys.get(i) != null) {
                aliveActivitys.get(i).finish();
            }
        }

    }

    public static Activity getTopActivity() {
        return aliveActivitys.get(aliveActivitys.size() - 1);
    }

    public static Activity getTopReduceActivity() {
        return aliveActivitys.get(aliveActivitys.size() - 2);
    }

    public static AppApplication getInstance() {
        return _context;
    }


    /**
     * Activity 生命周期监听，用于监控app前后台状态切换
     */
    ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }

        @Override
        public void onActivityStarted(Activity activity) {
            if (activityAount == 0) {
                //app回到前台
                isForeground = true;

                if (RegisterAct.getInstance() != null) {
                    if (UserPreference.getInstance(AppApplication.this).isLogin()) {
                        HttpUtils.getInstance().login(RegisterAct.getInstance().mHandler, UserPreference.getInstance(AppApplication.this).getLogonId(), UserPreference.getInstance(AppApplication.this).getPwd());
                    }
                }

            }
            activityAount++;
        }

        @Override
        public void onActivityResumed(Activity activity) {
        }

        @Override
        public void onActivityPaused(Activity activity) {
        }

        @Override
        public void onActivityStopped(Activity activity) {
            activityAount--;
            if (activityAount == 0) {
                isForeground = false;
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
        }
    };


}